package com.gem.netty.service.card;

import com.gem.entity.Card;
import com.gem.entity.Game;
import com.gem.enums.ClientMessageType;
import com.gem.enums.GemType;
import com.gem.enums.MessageColorType;
import com.gem.factory.game.GameFactory;
import com.gem.holder.ThreadLocalFactory;
import com.gem.netty.entity.CardInfo;
import com.gem.netty.entity.ClientContent;
import com.gem.netty.entity.CurrentUser;
import com.gem.pipeline.BusinessProcess;
import com.gem.pipeline.ProcessContext;
import com.gem.utils.R;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Author: Xhy
 * CreateTime: 2022-09-25 14:08
 *  尝试换取卡牌
 */
public class IsSwapCardAndReduceUserOwnGemService implements BusinessProcess<ClientContent> {
    @Override
    public void process(ProcessContext<ClientContent> context) {
        Game game = GameFactory.get(context.getData().getRoomId());
        CurrentUser user = game.getUsers().get(game.getCurrentIndex());
        CardInfo cardInfo = context.getData().getContent().getCardInfo();
        // 卡牌所需宝石
        Map<Integer, Integer> swapCardNeedGems =  Card.getCardGems(cardInfo.getNo());
        // 获取用户使用的宝石
        Map<Integer, Integer> userUseGems = cardInfo.getGems();
        // 获取用户使用的buff
        Map<Integer, Integer> userUserBuff = cardInfo.getGemCards();
        // 获取用户使用的听宝石
        Map<Integer, Integer> userUseTingGem = cardInfo.getReplaceGems();
        // 用户记录更新用户手里宝石
        Map<Integer,Integer> temp = new HashMap<>();
        // 用户自己的宝石
        TreeMap<Integer, Integer> userOwnGem = user.getGems();
        swapCardNeedGems.forEach((k,v)->{
            int gemNum =  (userUseGems != null && userUseGems.containsKey(k)) ? userUseGems.get(k) : 0 ;
            int cardNum = (userUserBuff != null && userUserBuff.containsKey(k)) ? userUserBuff.get(k) : 0 ;
            int tingGem = (userUseTingGem != null && userUseTingGem.containsKey(k)) ? userUseTingGem.get(k) : 0 ;
            int ownGems = gemNum + cardNum;
            int needGems = swapCardNeedGems.get(k);

            if (!trySwapCard(ownGems,needGems)){
                // 退回黄金宝石
                Map<Integer, Integer> replaceGems = cardInfo.getReplaceGems();
                AtomicInteger i = new AtomicInteger(0);
                replaceGems.forEach((k1,v1)->{
                    userOwnGem.put(k1,userOwnGem.get(k1)-v1);
                    i.getAndAdd(v1);
                });
                userOwnGem.put(GemType.GOLD.type,userOwnGem.get(GemType.GOLD.type)+i.get());
                context.setNeedBreak(true);
                context.setResult(R.error().data("color", MessageColorType.SYSTEM_MESSAGE.type).message("换不了卡牌,宝石不够").type(ClientMessageType.GET_CARD.type));
                return;
            }
            // 使用buff > 使用的宝石 ? 使用的宝石 : 使用的buff
            int t = cardNum > needGems ? needGems : cardNum;
            temp.put(k,needGems - t - tingGem);
        });
        if (!context.getNeedBreak()){
            // 用户宝石减少
            temp.forEach((k,v)->{
                Integer gem = (userOwnGem.get(k)==null)?0:userOwnGem.get(k);
                userOwnGem.put(k,gem-v);
            });

            // 用户黄金减少(使用黄金)
            Map<Integer, Integer> replaceGems = cardInfo.getReplaceGems();
            if (replaceGems.size() > 0){
                AtomicInteger number = new AtomicInteger(0);
                replaceGems.forEach((k,v)->{
                    number.getAndAdd(v);
                });
                Object tsCard = ThreadLocalFactory.get("isTsCard");
                Boolean isTsCard = false;
                if (tsCard instanceof Boolean){
                    isTsCard = (Boolean) tsCard;
                }
                if(isTsCard){
                    number.getAndDecrement();
                }

                userOwnGem.put(GemType.GOLD.type,userOwnGem.get(GemType.GOLD.type) - number.get());
            }
        }


    }
    private boolean trySwapCard(int ownGems,int needGems){
        return ownGems - needGems >=0;
    }




}
